<!--
  智慧社区管理系统 - 分配弹窗组件
  @author Wu.Liang
  @date 2025-06-26
  @version 3.0 - 根据系统设计调整方案重构
-->
<template>
  <el-dialog
    v-model="dialogVisible"
    title="分配投诉"
    width="500px"
    :close-on-click-modal="false"
    @close="handleClose"
  >
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
    >
      <el-form-item label="处理人员" prop="assigneeId">
        <el-select
          v-model="formData.assigneeId"
          placeholder="请选择处理人员"
          style="width: 100%"
        >
          <el-option
            v-for="item in assigneeList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      
      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="formData.remark"
          type="textarea"
          :rows="3"
          placeholder="请输入分配备注（可选）"
          maxlength="200"
          show-word-limit
        />
      </el-form-item>
    </el-form>
    
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="handleSubmit" :loading="loading">
          确认分配
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, watch, onMounted, computed } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import { commonApi } from '@/api/property'
import type { Complaint } from '../types/complaint'

// 作者: Wu.Liang

// Props
interface Props {
  modelValue: boolean
  complaintData: Complaint | null
  loading?: boolean
}

const props = withDefaults(defineProps<Props>(), {
  loading: false
})

// Emits
const emit = defineEmits<{
  'update:modelValue': [value: boolean]
  'confirm': [data: any]
}>()

// 响应式数据
const formRef = ref<FormInstance>()
const assigneeList = ref<any[]>([])

// 表单数据
const formData = reactive({
  assigneeId: undefined as number | undefined,
  remark: ''
})

// 表单验证规则
const formRules: FormRules = {
  assigneeId: [
    { required: true, message: '请选择处理人员', trigger: 'change' }
  ]
}

// 计算属性
const dialogVisible = computed({
  get: () => props.modelValue,
  set: (value) => emit('update:modelValue', value)
})

// 生命周期
onMounted(() => {
  loadAssigneeList()
})

// 监听器
watch(() => props.modelValue, (newVal) => {
  if (newVal) {
    resetForm()
  }
})

// 加载处理人员列表
const loadAssigneeList = async () => {
  try {
    const response = await commonApi.getUserList({})
    if (response.code === 200) {
      assigneeList.value = response.data
    }
  } catch (error) {
    console.error('加载处理人员列表失败:', error)
  }
}

// 重置表单
const resetForm = () => {
  Object.assign(formData, {
    assigneeId: undefined,
    remark: ''
  })
  formRef.value?.clearValidate()
}

// 提交表单
const handleSubmit = async () => {
  if (!formRef.value) return
  
  try {
    await formRef.value.validate()
    
    const submitData = {
      assigneeId: formData.assigneeId!,
      remark: formData.remark || undefined
    }
    
    emit('confirm', submitData)
  } catch (error) {
    console.error('表单验证失败:', error)
  }
}

// 关闭弹窗
const handleClose = () => {
  dialogVisible.value = false
}
</script>

<style scoped>
.dialog-footer {
  text-align: right;
}
</style> 